package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.enums.ShopStatus;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.system.domain.PStoreOrder;
import com.ruoyi.system.mapper.PStoreOrderInfoMapper;
import com.ruoyi.system.mapper.PStoreOrderMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.system.domain.bo.PStoreProductReplyBo;
import com.ruoyi.system.domain.vo.PStoreProductReplyVo;
import com.ruoyi.system.domain.PStoreProductReply;
import com.ruoyi.system.mapper.PStoreProductReplyMapper;
import com.ruoyi.system.service.IPStoreProductReplyService;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 评论Service业务层处理
 *
 * @author ruoyi
 * @date 2022-08-01
 */
@RequiredArgsConstructor
@Service
public class PStoreProductReplyServiceImpl implements IPStoreProductReplyService {

    private final PStoreProductReplyMapper baseMapper;
    private final PStoreOrderMapper orderMapper;
    private final SysUserMapper userMapper;
    private final PStoreOrderInfoMapper orderInfoMapper;

    /**
     * 查询评论
     */
    @Override
    public PStoreProductReplyVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询评论列表
     */
    @Override
    public TableDataInfo<PStoreProductReplyVo> queryPageList(PStoreProductReplyBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<PStoreProductReply> lqw = buildQueryWrapper(bo);
        Page<PStoreProductReplyVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询评论列表
     */
    @Override
    public List<PStoreProductReplyVo> queryList(PStoreProductReplyBo bo) {
        LambdaQueryWrapper<PStoreProductReply> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<PStoreProductReply> buildQueryWrapper(PStoreProductReplyBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<PStoreProductReply> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUid() != null, PStoreProductReply::getUid, bo.getUid());
        lqw.eq(bo.getOid() != null, PStoreProductReply::getOid, bo.getOid());
        lqw.eq(bo.getProductId() != null, PStoreProductReply::getProductId, bo.getProductId());
        lqw.eq(StringUtils.isNotBlank(bo.getReplyType()), PStoreProductReply::getReplyType, bo.getReplyType());
        lqw.eq(bo.getProductScore() != null, PStoreProductReply::getProductScore, bo.getProductScore());
        lqw.eq(bo.getServiceScore() != null, PStoreProductReply::getServiceScore, bo.getServiceScore());
        lqw.eq(StringUtils.isNotBlank(bo.getComment()), PStoreProductReply::getComment, bo.getComment());
        lqw.eq(StringUtils.isNotBlank(bo.getPics()), PStoreProductReply::getPics, bo.getPics());
        lqw.eq(StringUtils.isNotBlank(bo.getMerchantReplyContent()), PStoreProductReply::getMerchantReplyContent, bo.getMerchantReplyContent());
        lqw.eq(bo.getMerchantReplyTime() != null, PStoreProductReply::getMerchantReplyTime, bo.getMerchantReplyTime());
        lqw.eq(bo.getIsReply() != null, PStoreProductReply::getIsReply, bo.getIsReply());
        lqw.like(StringUtils.isNotBlank(bo.getNickname()), PStoreProductReply::getNickname, bo.getNickname());
        lqw.eq(StringUtils.isNotBlank(bo.getAvatar()), PStoreProductReply::getAvatar, bo.getAvatar());
        lqw.eq(StringUtils.isNotBlank(bo.getSku()), PStoreProductReply::getSku, bo.getSku());
        return lqw;
    }

    /**
     * 新增评论
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean insertByBo(PStoreProductReplyBo bo) {
        Long userId = LoginHelper.getUserId();
        SysUser sysUser = userMapper.selectUserById(userId);

        PStoreOrder storeOrder = orderMapper.selectOne(new LambdaQueryWrapper<PStoreOrder>().eq(PStoreOrder::getOrderId, bo.getUnique()));
        if (ObjectUtil.isEmpty(storeOrder)) {
            throw new ServiceException("非法订单！");
        }
        // 填充评论
        PStoreProductReply add = BeanUtil.toBean(bo, PStoreProductReply.class);
        add.setOid(storeOrder.getId());
        add.setAvatar(sysUser.getWxAvatar());
        add.setNickname(sysUser.getNickName());
        add.setUid(userId);
//        Integer count = checkIsReply(add);
        // 更改订单状态
        storeOrder.setStatus(ShopStatus.FINISH.getValue());
        orderMapper.updateById(storeOrder);

        return baseMapper.insert(add) > 0;
    }

    /**
     * 检测当前商品是否可以评论
     * @author Mr.Zhang
     * @since 2020-06-03
     * @return Integer
     */
    private Integer checkIsReply(PStoreProductReply reply) {
//        List<PStoreOrderInfo> orderInfoList = orderInfoMapper.selectList(new LambdaQueryWrapper<PStoreOrderInfo>().eq(PStoreOrderInfo::getOrderId, reply.getOid()));
//        if (CollectionUtil.isEmpty(orderInfoList)) {
//            throw new ServiceException("没有找到该商品信息！");
//        }
//
//        Long productId = reply.getProductId();
//        for (PStoreOrderInfo orderInfo : orderInfoList) {
//            Long productId1 = orderInfo.getProductId();
//        }
        return null;
    }

    /**
     * 修改评论
     */
    @Override
    public Boolean updateByBo(PStoreProductReplyBo bo) {
        PStoreProductReply update = BeanUtil.toBean(bo, PStoreProductReply.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(PStoreProductReply entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除评论
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
